package com.hins.queue;

import java.util.Scanner;

public class CircleArrayQueue {
    public static void main(String[] args) throws Exception {
        CircleArray circleArray = new CircleArray(3);
        Scanner sc = new Scanner(System.in);
        boolean loop = true;
        while (loop) {

            System.out.println("s(show)：显示队列");
            System.out.println("e(exit)：退出队列");
            System.out.println("a(add)：添加数据到队列");
            System.out.println("g(get)：从队列中取出数据");
            System.out.println("h(head)：查看队列头的数据");
            String next = sc.next();
            switch (next) {
                case "s":
                    circleArray.showQueue();
                    break;
                case "a":
                    System.out.println("请输入一个数");
                    int i = sc.nextInt();
                    circleArray.addQueue(i);
                    break;
                case "g":

                    try {
                        int res = circleArray.removeQueue();
                        System.out.println("取出的数据是 " + res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case "h":
                    try {
                        int res = circleArray.headQueue();
                        System.out.println("队列头数据是" + res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;

                case "e":
                    sc.close();
                    loop = false;
                    break;
            }

        }
        System.out.println("程序退出");

    }
}

class CircleArray {
    private int arr[];
    private int maxSize;
    private int rear;//队尾默认为0，指向最后一个元素的下一个位置
    private int front;//队头默认为0，指向队列第一个元素

    public CircleArray(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
    }

    public boolean isFull() {

        return (rear + 1) % maxSize == front;
    }

    public boolean isEmpty() {
        return rear == front;
    }

    public void addQueue(int value) {
        if (isFull()) {
            System.out.println("队列为满，不能添加");
            return;
        }
        arr[rear] = value;
        rear = (rear + 1) % maxSize;

    }

    public int removeQueue() throws Exception {
        if (isEmpty()) {
            throw new Exception("队列为空，不能取数据");
        }
        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;
    }

    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列为空，没有数据");
        }
        for (int i = front; i < front + ((rear + maxSize - front) % maxSize); i++) {
            System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);

        }
    }

    public int headQueue() throws Exception {

        if (isEmpty()) {
            throw new Exception("队列为空，没有数据");
        }

        return arr[front];
    }
}
